package com.jfinal.code.eclipse.plugin.wizards.page;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;

import com.jfinal.code.eclipse.plugin.generator.dto.CustomValueDTO;
import com.jfinal.code.eclipse.plugin.generator.dto.JFinalValueDTO;
import com.jfinal.code.eclipse.plugin.i18n.I18n;
import com.jfinal.code.eclipse.plugin.wizards.prop.Key;
import com.jfinal.code.eclipse.plugin.wizards.prop.PropertiesUtils;
import com.jfinal.code.eclipse.plugin.wizards.ui.MakeCodeUI;

/**
 * 代码生成策略引导页
 * @author yaolin
 *
 */
public class MakeCodeWizardPage extends WizardPage {

	private static final String PAGE_NAME = "MakeCodeWizardPage";
	private ISelection selection;
	private boolean inited = false;
	private TabFolder tabFolder; // 代码生成策略选项卡
	// JFinal Tab
	private Text codeText; // java代码根目录
	private Text prefixText; // 表前缀
	private Text baseModelPackageNameText; // baseModel包名
	private Text modelPackageNameText; // model包名
	private Button genDaoInModelButton; // 在model中生成dao
	private Button genDictButton; // 生成字典文件
	// http://www.jfinal.com/share/211
	// http://www.oschina.net/question/2276614_2147333
	private Button sqlServerFixErrorButton; // SqlServer 2008 报错问题
	// Custom Tab
	private Text customJarFileText; // 自定jar
	private Text customImplClassNameText; // 自定是实现类名
	private Text basePackNameText; // 基本包名
	private Text templateDirText; // 模板文件根路径
	private Text javaText; // java文件的根路径
	private Text resourceText; // 配置文件的根路径
	private Text jsText; // js 文件的根路径
	private Text viewText; // 视图文件的根路径
	
	// JFinal-Custom Tab  // TODO 自定义拓展JFinal的Generator
//	private Text jfinalCustomJarFileText;
//	private Text jfinalCustomClassNameText;
	
	
	// 10-Code Tab
	public MakeCodeWizardPage(ISelection selection) {
		super(PAGE_NAME);
		setTitle(I18n.makeCodeWp_title);
		setDescription(I18n.makeCodeWp_description);
		this.selection = selection;
	}

	@Override
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout(1, false);
		container.setLayout(layout);
		
		// ui
		MakeCodeUI ui = new MakeCodeUI(this, getShell());
		
		tabFolder = ui.createTabFolder(container);
		tabFolder.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL));

		// -------JFinal-----------
		TabItem jfinalTabItem = new TabItem(tabFolder, SWT.NULL);
		jfinalTabItem.setText("JFinal");
		Composite jfinalTabContainer = new Composite(tabFolder, SWT.NULL);
		GridLayout jfinalTabLayout = new GridLayout(3, false);
		jfinalTabContainer.setLayout(jfinalTabLayout);
		jfinalTabLayout.marginTop = 30;
		jfinalTabLayout.marginRight = 20;
		jfinalTabLayout.marginLeft = 20;
		jfinalTabLayout.horizontalSpacing = 10;
		jfinalTabLayout.verticalSpacing = 10;
		jfinalTabItem.setControl(jfinalTabContainer);
		{
			codeText = ui.createCodeText(jfinalTabContainer);
			prefixText = ui.createPrefixText(jfinalTabContainer);
			baseModelPackageNameText = ui.createBaseModelPackageNameText(jfinalTabContainer);
			modelPackageNameText = ui.createModelPackageNameText(jfinalTabContainer);
			genDaoInModelButton = ui.createGenDaoInModelButton(jfinalTabContainer);
			genDictButton = ui.createGenDictButton(jfinalTabContainer);
			sqlServerFixErrorButton = ui.createSqlServerFixErrorButton(jfinalTabContainer);
		}
		// ------------------------
		
		// -------Custom-----------
		TabItem customTabItem = new TabItem(tabFolder, SWT.NULL);
		customTabItem.setText("Custom");
		Composite customTabContainer = new Composite(tabFolder, SWT.NULL);
		GridLayout customTabLayout = new GridLayout(3, false);
		customTabContainer.setLayout(customTabLayout);
		customTabLayout.marginTop = 15;
		customTabLayout.horizontalSpacing = 5;
		customTabLayout.verticalSpacing = 5;
		customTabItem.setControl(customTabContainer);
		{
			customJarFileText = ui.createCustomJarFileText(customTabContainer);
			customImplClassNameText = ui.createCustomImplClassNameText(customTabContainer);
			basePackNameText = ui.createBasePackNameText(customTabContainer);
			templateDirText = ui.createTemplateDirText(customTabContainer);
			javaText = ui.createJavaText(customTabContainer);
			resourceText = ui.createResourceText(customTabContainer);
			jsText = ui.createJsText(customTabContainer);
			viewText = ui.createViewText(customTabContainer);
		}
		// ------------------------
		
		// -----jfinal-custom------
		
		// -------10-Code-----------
//		TabItem lnTabItem = new TabItem(tabFolder, SWT.NULL);
//		lnTabItem.setText("10-Code");
//		Composite lnTabContainer = new Composite(tabFolder, SWT.NULL);
//		lnTabContainer.setLayout(new FillLayout());
//		Label lb = new Label(lnTabContainer, SWT.CENTER);
//		StringBuffer sb = new StringBuffer(System.lineSeparator()); // JDK1.7+
//		sb.append("老夫聊发少年狂，半夜想喝疙瘩汤。").append("一树梨花压海棠，酸菜白肉汆血肠。").append(System.lineSeparator());
//		sb.append("不思量，自难忘，啃上一根大骨棒。").append("怎得梅花扑鼻香，东北正宗溜肥肠。").append(System.lineSeparator());
//		sb.append("风萧萧兮易水寒，壮士想吃溜肉段。").append("安得广厦千万间，一起来做烤冷面。").append(System.lineSeparator());
//		sb.append("别人笑我太疯癫，松仁玉米地三鲜。").append("月落乌啼霜满天，东北栏杆有点甜。").append(System.lineSeparator());
//		sb.append("天若有情天亦老，就想每天吃烧烤。").append("二月春风似剪刀，冰糖葫芦粘豆包。").append(System.lineSeparator());
//		sb.append("小扣柴扉久不开，不如一起腌酸菜。").append("一枝红杏出墙来，两人整个杀猪菜。").append(System.lineSeparator());
//		sb.append("故人西辞黄鹤楼，前天没吃锅包肉。").append("问君能有几多愁，昨天没吃锅包肉。").append(System.lineSeparator());
//		sb.append("劝君更尽一杯酒，今天没吃锅包肉。").append("路见不平一声吼，各种想吃锅包肉。").append(System.lineSeparator());
//		sb.append(System.lineSeparator());
//		sb.append("罗浮山下四时春，整锅东北大乱炖。").append("洛阳亲友如相问，就说点了芹菜粉。").append(System.lineSeparator());
//		sb.append("同是天涯沦落人，冻梨红薯加榛仁。").append("小荷才露尖尖角，排骨最好炖豆角。").append(System.lineSeparator());
//		sb.append("在天愿作比翼鸟，在地愿吃酸菜饺。").append("铜雀春深锁二乔，来碗猪肉炖粉条。").append(System.lineSeparator());
//		sb.append("两岸猿声啼不住，想吃小鸡炖蘑菇。").append("不识庐山真面目，竟是鱼头炖豆腐。").append(System.lineSeparator());
//		sb.append("人生在世不称意，来份酥软炸里脊。").append("君问归期未有期，鸡架雪花配拉皮。").append(System.lineSeparator());
//		sb.append("铜雀春深锁二乔，来碗猪肉炖粉条。").append("问君能有几多愁，昨天没吃锅包肉。").append(System.lineSeparator());
//		sb.append("劝君更尽一杯酒，今天没吃锅包肉。").append("路见不平一声吼，各种想吃锅包肉。");
//		lb.setText(sb.toString());
//		lnTabItem.setControl(lnTabContainer);
		// ------------------------
		setControl(container);
		inited = true;
	}

	/**
	 * 获取JFinal代码生成策略所填写的字段值
	 */
	public JFinalValueDTO getJFinalValue() {
		JFinalValueDTO dto = new JFinalValueDTO();
		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
		IResource res = root.findMember(new Path(codeText.getText().trim()));
		if (res.exists() && (res instanceof IContainer)) {
			dto.setCodePath(res.getLocationURI().toString().replace("file:/", ""));
		}
		dto.setBaseModelPackageName(baseModelPackageNameText.getText().trim());
		dto.setModelPackageName(modelPackageNameText.getText().trim());
		dto.setTablePrefix(prefixText.getText().trim());
		dto.setGenDaoInModel(genDaoInModelButton.getSelection());
		dto.setGenDict(genDictButton.getSelection());
		dto.setSqlServerFixError(sqlServerFixErrorButton.getSelection());
		return dto;
	}
	
	/**
	 * 获取自定义代码生成策略所填写的字段值
	 */
	public CustomValueDTO getCustomValue() {
		CustomValueDTO dto = new CustomValueDTO();
		dto.setTemplatePath(templateDirText.getText().trim());
		dto.setBasePackageName(basePackNameText.getText().trim());
		dto.setCustomJar(customJarFileText.getText().trim());
		dto.setCustomImplClassName(customImplClassNameText.getText().trim());
		
		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
		IResource res = root.findMember(new Path(javaText.getText().trim()));
		if (res.exists() && (res instanceof IContainer)) {
			dto.setJavaPath(res.getLocationURI().toString().replace("file:/", ""));
		}
		
		res = root.findMember(new Path(resourceText.getText().trim()));
		if (res.exists() && (res instanceof IContainer)) {
			dto.setResourcePath(res.getLocationURI().toString().replace("file:/", ""));
		}
		
		res = root.findMember(new Path(jsText.getText().trim()));
		if (res.exists() && (res instanceof IContainer)) {
			dto.setJsPath(res.getLocationURI().toString().replace("file:/", ""));
		}
		
		res = root.findMember(new Path(viewText.getText().trim()));
		if (res.exists() && (res instanceof IContainer)) {
			dto.setViewPath(res.getLocationURI().toString().replace("file:/", ""));
		}
		return dto;
	}
	
	/**
	 * 重写setVisible获取配置文件的信息并填写到控件中
	 */
	@Override
	public void setVisible(boolean visible) {
		prefixText.setText(PropertiesUtils.get(Key.jfinal_tablePrefix, ""));
		baseModelPackageNameText.setText(PropertiesUtils.get(Key.jfinal_baseModelPackageName, ""));
		modelPackageNameText.setText(PropertiesUtils.get(Key.jfinal_modelPackageName, ""));
		genDaoInModelButton.setSelection(PropertiesUtils.getBoolean(Key.jfinal_generateDaoInModel, true));
		genDictButton.setSelection(PropertiesUtils.getBoolean(Key.jfinal_generateDict, true));
		
		customJarFileText.setText(PropertiesUtils.get(Key.custom_customJar, ""));
		customImplClassNameText.setText(PropertiesUtils.get(Key.custom_implementClassName, ""));
		basePackNameText.setText(PropertiesUtils.get(Key.custom_basePackageName, ""));
		templateDirText.setText(PropertiesUtils.get(Key.custom_templateDir, ""));
		MakeCodeUI.Validator.validatorEmptyString(this);
		super.setVisible(visible);
	}
	
	// getter
	public ISelection getSelection() {
		return selection;
	}
	public boolean isInited() {
		return inited;
	}
	public TabFolder getTabFolder() {
		return tabFolder;
	}
	public Text getCodeText() {
		return codeText;
	}
	public Text getPrefixText() {
		return prefixText;
	}
	public Text getBaseModelPackageNameText() {
		return baseModelPackageNameText;
	}
	public Text getModelPackageNameText() {
		return modelPackageNameText;
	}
	public Button getGenDaoInModelButton() {
		return genDaoInModelButton;
	}
	public Button getGenDictButton() {
		return genDictButton;
	}
	public Button getSqlServerFixErrorButton() {
		return sqlServerFixErrorButton;
	}
	//
	public Text getCustomJarFileText() {
		return customJarFileText;
	}
	public Text getCustomImplClassNameText() {
		return customImplClassNameText;
	}
	public Text getBasePackNameText() {
		return basePackNameText;
	}
	public Text getTemplateDirText() {
		return templateDirText;
	}
	public Text getJavaText() {
		return javaText;
	}
	public Text getResourceText() {
		return resourceText;
	}
	public Text getJsText() {
		return jsText;
	}
	public Text getViewText() {
		return viewText;
	}
}
